package cn.vire.electric.simulation.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.stereotype.Component;

import java.net.URI;

/**
 * @auther jw
 * @date 2022-02-26
 */
@Component
@Slf4j
@Data
public class JdbcConfigInfo {
    private String host;
    private int port;
    private String database;
    private String username;
    private String password;

    public JdbcConfigInfo(DataSourceProperties properties) {
        try {
            URI uri = URI.create(properties.getUrl().replace("jdbc:", ""));
            this.host = uri.getHost();
            this.port = uri.getPort();
            if (uri.getPath()!=null) {
                this.database = uri.getPath().replace("/", "");
            }
            this.username = properties.getUsername();
            this.password = properties.getPassword();
        } catch (Exception e) {
            log.error("获取数据库配置异常", e);
        }
    }
}
